﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Services;
using WebUI.Models;
using Entities;
using System.Text;

namespace WebUI.Controllers
{
    public partial class FiltersController : Controller
    {
        #region Member Variables

        readonly IFilterService filterService;

        #endregion

        #region Constructor

        public FiltersController(IFilterService filterService)
        {
            this.filterService = filterService;
        }

        #endregion

        #region Action Methods

        #region FileTypes

        public virtual PartialViewResult OpenFileTypesDialog(string selectedFileTypes)
        {
            var list = filterService.GetFileTypes();
            var model = new CheckBoxListModel
            {
                AllData = list,
                SelectedData = !string.IsNullOrEmpty(selectedFileTypes) ? selectedFileTypes.ToUpper().Split(new char[1] { ',' }) : null
            };
            return PartialView(MVC.Filters.Views._FileTypes, model);
        }

        [HttpPost]
        public void FileTypesDialogResult(string fileTypes, FilterOperations filter)
        {
            filter.FileType = fileTypes;
        }

        [HttpPost]
        public virtual PartialViewResult SearchFileTypes(string token, string All, string Selected, FilterOperations filter)
        {
            var list = All.Split(new char[1] { ',' });
            var searchResult = list.Where(c => c.ToUpper().Contains(token.ToUpper()));
            var model = new CheckBoxListModel
            {
                AllData = searchResult,
                SelectedData = !string.IsNullOrEmpty(filter.FileType) ? filter.FileType.Split(new char[1] { ',' }).ToList() : new List<string>()
            };
            model.SelectedData = model.SelectedData.Union(!string.IsNullOrEmpty(Selected) ? Selected.Split(new char[1] { ',' }).ToList() : new List<string>());
            return PartialView(MVC.Filters.Views._FileTypesTable, model);
        }

        #endregion

        #region Hosts

        public virtual ActionResult OpenHostsDialog(FilterOperations filter)
        {
            var list = filterService.GetHosts();
            var model = new CheckBoxListModel
            {
                AllData = list,
                SelectedData = !string.IsNullOrEmpty(filter.Host) ? filter.Host.Split(new char[1] { ',' }) : null
            };
            return PartialView(MVC.Filters.Views._Hosts, model);
        }

        [HttpPost]
        public virtual JsonResult HostsDialogResult(IEnumerable<string> hosts, FilterOperations filter)
        {
            filter.Host = Common.ConstructDelimitedString(hosts);
            return Json(JsonResponseFactory.SuccessResponse(filter.Host), JsonRequestBehavior.DenyGet);
        } 

        #endregion

        #endregion
    }
}